home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / NeXT-Icons / next-icon@gun.com / Apps / ImagePortfolio / apputils.subproj / appUtils.m < prev    next >
Encoding:
Text File  |  1993-06-03  |  3.6 KB  |  113 lines

  1. // -------------------------------------------------------------------------------------
  2. // misc application utilities
  3. // -------------------------------------------------------------------------------------
  4. // Permission is granted to freely redistribute this source code, and to use fragments
  5. // of this code in your own applications if you find them to be useful.  This class,
  6. // along with the source code, come with no warranty of any kind, and the user assumes
  7. // all responsibility for its use.
  8. // -------------------------------------------------------------------------------------
  9.  
  10. #import <appkit/appkit.h>
  11. #import <stdio.h>
  12. #import <string.h>
  13. #import <ctype.h>
  14. #import <libc.h>
  15. #import <sys/types.h>
  16. #import <sys/stat.h>
  17. #import <sys/param.h>
  18. #import <sys/dir.h>
  19. #import <defaults/defaults.h>
  20.  
  21. // -------------------------------------------------------------------------------------
  22. // Application path functions
  23.  
  24. /* return path to application ("/appName" is removed from path) */
  25. /* Note: This should be called at the beginning of the app to cache the app path name */
  26. const char *XAppPath()
  27. {
  28.     static char    *appPathName = (char*)nil;
  29.     if (!appPathName) {
  30.         char *p, path[MAXPATHLEN + 1];
  31.         if (NXArgv[0][0] == '/') strcpy(path, NXArgv[0]);
  32.         else { 
  33.             getwd(path);
  34.             strcat(path, "/");
  35.             strcat(path, NXArgv[0]);
  36.         }
  37.         if ((p = rindex(path, '/')) && (p != path)) *p = 0;
  38.         appPathName = strcpy((char*)malloc(strlen(path) + 1), path);
  39.     }
  40.     return appPathName;
  41. }
  42.  
  43. /* return fully qualified file path name */
  44. const char *XFullPath(const char *fileName)
  45. {
  46.     char    path[MAXPATHLEN + 1] = { 0 }, *p = path;
  47.     if (*fileName != '/') p += strlen(strcpy(path, XAppPath()));
  48.     if ((p > path) && (*(p - 1) != '/')) *p++ = '/';
  49.     strcpy(p, fileName);
  50.     return NXCopyStringBuffer(path);
  51. }
  52.  
  53. // -------------------------------------------------------------------------------------
  54. // change/set application icon
  55.  
  56. void XSetAppIcon(void *imageV)
  57. {
  58.     id                image = (id)imageV;
  59.     id              iconWin = [NXApp appIcon];      
  60.     NXPoint         iconOrigin = { 0.0, 0.0 };
  61.     NXSize          imageSize;
  62.     static id       tileImage = (id)nil;
  63.     static NXSize   tileSize = { 0.0, 0.0 };
  64.  
  65.     /* init dock icon tile */
  66.     if (!tileImage && (tileImage = [NXImage findImageNamed:"NXAppTile"])) {
  67.         [tileImage getSize:&tileSize];
  68.     }
  69.  
  70.     /* redraw AppIcon */
  71.     [[iconWin contentView] lockFocus];
  72.     PSsetinstance(FALSE);
  73.     [tileImage composite:NX_COPY toPoint:&iconOrigin];
  74.     [tileImage getSize:&tileSize];
  75.     [image getSize:&imageSize];
  76.     iconOrigin.x = (tileSize.width  - imageSize.width ) / 2.0;
  77.     iconOrigin.y = (tileSize.height - imageSize.height) / 2.0;
  78.     [image composite:NX_SOVER toPoint:&iconOrigin];
  79.     [[iconWin contentView] unlockFocus];
  80.     [iconWin display];
  81.   
  82. }
  83.  
  84. // -------------------------------------------------------------------------------------
  85. // Launch applications
  86.  
  87. /* launch application with specified file */
  88. int XLaunchApplication(const char *applName, const char *hostName, const char *openFile)
  89. {
  90.   port_t        port;
  91.   int            ok = YES;
  92.   char            *host = (hostName && *hostName)? hostName : (char*)nil;
  93.   if ((port = NXPortFromName(applName, host)) == PORT_NULL) return -1;
  94.   [[NXApp appSpeaker] setSendPort:port];
  95.   [[NXApp appSpeaker] openFile:openFile ok:&ok];
  96.   return 0;
  97. }
  98.  
  99. /* kill all tasks currently running with the given name */
  100. int XKillNamedTask(char *name)
  101. {
  102.   char    line[256], processName[256];
  103.   int    pid;
  104.   FILE    *fpsys = popen("ps -x", "r");
  105.   if (!fpsys) return -1;
  106.   while (fgets(line, 256, fpsys))  {
  107.     sscanf(line, "%d %*s %*s %*s %s", &pid, processName);
  108.     if (!strcmp(processName, name)) kill(pid, SIGKILL);
  109.   }
  110.   pclose(fpsys);
  111.   return 0;
  112. }
  113.